package com.winshare.saleAssistant.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winshare.saleAssistant.dao.TUserMapper;
import com.winshare.saleAssistant.domain.dto.req.PageQueryDTO;
import com.winshare.saleAssistant.domain.dto.req.UserQueryDTO;
import com.winshare.saleAssistant.model.TUser;
import com.winshare.saleAssistant.modelVo.LoginCountVo;
import com.winshare.saleAssistant.service.ITUserService;
import com.winshare.saleAssistant.util.ToolUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户基础信息 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2020-11-09
 */
@Service("TUserServiceImpl")
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements ITUserService {

    @Override
    public List<TUser> selectUser() {
        return this.baseMapper.selectUser();
    }

    @Override
    public com.winshare.saleAssistant.util.Page<TUser> selectByCondition(UserQueryDTO userQueryDTO) {
        QueryWrapper<TUser> queryWrapper = new QueryWrapper<>();
        if (!ToolUtil.isEmpty(userQueryDTO.getProductCode())) {
            queryWrapper.eq("product_code", userQueryDTO.getProductCode());
        }
        if (!ToolUtil.isEmpty(userQueryDTO.getCityName())) {
            queryWrapper.eq("city_name", userQueryDTO.getCityName());
        }
        if (!ToolUtil.isEmpty(userQueryDTO.getSchoolName())) {
            queryWrapper.like("school_name", userQueryDTO.getSchoolName());
        }
        queryWrapper.isNotNull("school_code");
        queryWrapper.ne("school_code", "#N/A");
        int totalCount = this.baseMapper.selectCount(queryWrapper);
        Integer pageIndex = userQueryDTO.getPageIndex();
        Integer pageSize = userQueryDTO.getPageSize();
        if (0 == totalCount) {
            return new com.winshare.saleAssistant.util.Page<>(pageIndex, pageSize, totalCount, new ArrayList<>());
        } else {
            return new com.winshare.saleAssistant.util.Page<>(pageIndex, pageSize, totalCount, this.baseMapper.selectByCondition(userQueryDTO));
        }
    }

    /**
     * 根据学校名字修改学校编号
     *
     * @param SchoolName
     * @param SchoolCode
     */
    @Override
    public Integer updateUserSchoolCode(String SchoolName, String SchoolCode) {
        UpdateWrapper<TUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("school_name", SchoolName);

        TUser user = new TUser();
        user.setSchoolCode(SchoolCode);

        return baseMapper.update(user, updateWrapper);
    }

    @Override
    public com.winshare.saleAssistant.util.Page<Map<String, Object>> getNotMatchUserByPage(PageQueryDTO pageQueryDTO) {
        int totalCount = this.baseMapper.getNotMatchTotal();
        Integer pageIndex = pageQueryDTO.getPageIndex();
        Integer pageSize = pageQueryDTO.getPageSize();
        if (0 == totalCount) {
            return new com.winshare.saleAssistant.util.Page<>(pageIndex, pageSize, totalCount, new ArrayList<>());
        } else {
            return new com.winshare.saleAssistant.util.Page<>(pageIndex, pageSize, totalCount, this.baseMapper.getNotMatchList(pageQueryDTO));
        }
    }

    @Override
    public Integer getTUserCount(Date starTime, Date endTime, String productName) {
        return this.baseMapper.getTUserCount(starTime, endTime, productName, "");
    }

    @Override
    public List<TUser> getActivationTotal(String areaName, String productName, Date starTime, Date endTime) {
        return this.baseMapper.getActivationTotal(areaName, productName, starTime, endTime);
    }

    @Override
    public List<TUser> getLoginTotal(String areaName, String productName, Date starTime, Date endTime) {
        return this.baseMapper.getActivationTotal(areaName, productName, starTime, endTime);
    }

    @Override
    public int getAccountTotal(String areaName, String productName, Date starTime, Date endTime) {
        return this.baseMapper.getAccountTotal(areaName, productName, starTime, endTime);
    }

    @Override
    public List<LoginCountVo> getSexProportion(String provinceName, String cityName, String areaName, String productName, Date starTime, Date endTime) {
//        int userTotal = this.baseMapper.getUserTotal(provinceName, cityName, areaName, productName, starTime, endTime);
        List<LoginCountVo> sexProportion = this.baseMapper.getSexProportion(provinceName, cityName, areaName, productName, starTime, endTime);
//        for (LoginCountVo loginCountVo : sexProportion) {
//            loginCountVo.setTotal(loginCountVo.getTotal() / userTotal);
//        }
        return sexProportion;
    }

    @Override
    public List<TUser> getSubjectCount(String provinceName, String cityName, String areaName, String productName, Date starTime, Date endTime) {
//        int total = this.baseMapper.getUserTotal(provinceName, cityName, areaName, productName, starTime, endTime);
        List<TUser> subjectCount = this.baseMapper.getSubjectCount(provinceName, cityName, areaName, productName, starTime, endTime);
//        List<TUser> list = new ArrayList<>();
//        if (subjectCount.size() > 1) {
//            TUser t = new TUser();
//            for (TUser tUser : subjectCount) {
//                t.setSubject(tUser.getSubject());
//                t.setTotal(tUser.getTotal() / total);
//                list.add(t);
//            }
//            return list;
//        }
        return subjectCount;
    }

    @Override
    public List<TUser> getLearSectionCount(String provinceName, String cityName, String areaName, String productName, Date starTime, Date endTime) {
//        int total = this.baseMapper.getUserTotal(provinceName, cityName, areaName, productName, starTime, endTime);
        List<TUser> learSectionCount = this.baseMapper.getLearSectionCount(provinceName, cityName, areaName, productName, starTime, endTime);
//        List<TUser> list = new ArrayList<>();
//        if (learSectionCount.size() > 1) {
//            TUser t = new TUser();
//            for (TUser tUser : learSectionCount) {
//                t.setSubject(tUser.getSubject());
//                t.setTotal(tUser.getTotal() / total);
//                list.add(t);
//            }
//            return list;
//        }
        return learSectionCount;
    }

    @Override
    public List<TUser> getUserCountTime(String orderBy, String schoolName, String provinceName, String cityName, String areaName, String productName, Date starTime, Date endTime) {
        return this.baseMapper.getUserCountTime(provinceName, cityName, areaName, productName, starTime, endTime, orderBy, schoolName);
    }

    @Override
    public Map<String, Object> getPopAge(String provinceName, String cityName, String areaName, String productName, Date starTime, Date endTime) {
        return this.baseMapper.getPopAge(provinceName, cityName, areaName, productName, starTime, endTime);
    }

    @Override
    public String getSynchronousTime() {
        return this.baseMapper.getSynchronousTime();
    }


}
